package com.ptswitch.material.pages.contract;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.SplitPageInfo;

import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.pages.base.MaterialBasePage;
import com.ptswitch.material.service.ContractService;

public class ListContract extends MaterialBasePage {
    @Inject
    private ContractService contractSer;
    @Property
    private List<Contract> list;
    @Property
    private SplitPageInfo page = new SplitPageInfo();
    private String limit = "all";

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(String limit, long size, long first) {
        checkUserIsContract();
        this.limit = limit;
        page.setPageSize(size);
        page.setCurrentPage(first);
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return new Object[] { limit, page.getPageSize(), page.getCurrentPage() };
    }

    @SetupRender
    void display() {
        checkUserIsContract();
        page.setRowCount(contractSer.countAll());
        if ("visitor".equals(limit)) {
            list = contractSer.findByVisitor(getVisitor(), page.getCurrentRow(), page.getPageSize());
        } else if (limit.equals("cancel")) {
            list = contractSer.findAllByContractStatus(Contract.Status_Cancel, page.getCurrentRow(), page.getPageSize());
        } else if (limit.equals("ok")) {
            list = contractSer.findAllByContractStatus(Contract.Status_Ok, page.getCurrentRow(), page.getPageSize());
        } else if (limit.equals("unok")) {
            list = contractSer.findAllByContractStatus(Contract.Status_UnOk, page.getCurrentRow(), page.getPageSize());
        } else {
            list = contractSer.findAll(page.getCurrentRow(), page.getPageSize());
        }
    }

}
